''' 
 Program to read data sent from tiny45 board,
 count and display the number of time key has been
 pressed.

 Todo: display time between last 2 key presses
'''

import serial #import serial library
import time #import time library

count = 0		#init counter to 0
oldtime = time.time()		#previous time

ser = serial.Serial('COM6', 9600, timeout=0)


while 1:
	while (ser.inWaiting() == 0):   #wait for serial data to be available
		pass		#do nothing
	newtime = time.time()			#read current time
	keypress = ser.readline()		#read text from serial port
	if (keypress == '1'):
		count += 1		#increment counter
	print "Number of keypresses: {}".format(count)
	elapsed = newtime - oldtime
	print "Time between keypress: {}".format(elapsed)	#display time between keypress
	oldtime = newtime
